package gui;
import chatsystemTDa2.*;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.ArrayList;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.KeyEventDispatcher;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileNameExtensionFilter;

import controller.ChatController;
import model.RemoteUser;
/**
 * conversation window for each user that we want to communicate
 * link to the chat GUI and a remote user
 */
public class ConversationWindow extends JFrame implements ActionListener, WindowListener{
	private ChatGUI chatGUI;
	private RemoteUser user;
	
	private JTextArea conversationArea;
    private JTextArea userInputArea ;
	private JButton bSend;
	private JButton bSendFile;
	
	public ConversationWindow(RemoteUser user, ChatGUI chatGUI) {
		this.user = user;
		this.chatGUI = chatGUI;
		this.initComponents();
	}

	/**
	 * initialize a conversation window
	 */
	public void initComponents(){
		this.setTitle("Conversation with "+ this.user.getUsername());
		this.setLayout(new BorderLayout());
		
		//configure the msg to send field
		this.userInputArea = new JTextArea(10,10);
		JScrollPane scrollPaneInput = new JScrollPane(userInputArea);
		scrollPaneInput.setSize(10,10 );
		// Lines will be wrapped if they are too long to fit within the
	    // allocated width
		userInputArea.setLineWrap(true);
	    // Lines will be wrapped at word boundaries (whitespace) if they are
	    // too long to fit within the allocated width
		userInputArea.setWrapStyleWord(true);
		// We also want a vertical scroll bar on our pane, as text is added to it
	    scrollPaneInput.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
	    
		//configure the chat area
		this.conversationArea = new JTextArea(30, 10);
		JScrollPane scrollPane = new JScrollPane(conversationArea);
	    scrollPane.setSize(30, 10);
	    // Lines will be wrapped if they are too long to fit within the
	    // allocated width
	    conversationArea.setLineWrap(true);
	    // Lines will be wrapped at word boundaries (whitespace) if they are
	    // too long to fit within the allocated width
	    conversationArea.setWrapStyleWord(true);
	    // Assuming this is the chat client's window where we read text sent out
	    // and received, we don't want our Text Area to be editable!
	    conversationArea.setEditable(false);
	    // We also want a vertical scroll bar on our pane, as text is added to it
	    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
	    
		this.bSend = new JButton("Send");
		this.bSendFile = new JButton("Send File");
		bSend.addActionListener(this);
		this.setLayout(new FlowLayout());
	    //adds and centers the text field to the frame
	    this.add(scrollPaneInput, SwingConstants.CENTER);
	    //adds and centers the scroll pane to the frame
	    this.add(scrollPane, SwingConstants.CENTER);
	    
	    this.add("South",bSend);
	    this.add("South",bSendFile);
	    bSendFile.addActionListener(this);
	    this.setFocusable(true);
		this.pack();
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.addWindowListener(this);
	}
	
	/**
	 * Action when we click a button
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(bSend)){
	        String textToSend = userInputArea.getText();
			//SEND MESSAGE
			chatGUI.send(userInputArea.getText(), user);
			//UPDATE CONVERSATION WHEN WE SEND A MESSAGE
	        if (textToSend != null) {
	        	updateConversation(textToSend, "Me");
	        }
		}
		if (e.getSource().equals(bSendFile)){
			chatGUI.chooseFileDialog();
		}
	}    
	
	/**
	 * update the conversation when we send a message or receive a message
	 * @param msg a text message
	 * @param userName the user name of who sends this text message
	 */
	public void updateConversation(String msg, String userName){
        //We append the text from the user
    	conversationArea.append(userName+": "+ msg + "\n");
        //The pane auto-scrolls with each new response added
        conversationArea.setCaretPosition(conversationArea.getDocument().getLength());
        //We reset our text field to "" each time the user presses Enter
        userInputArea.setText("");
	}
	
	/**
	 * getter for RemoteUser
	 * @return Remote User of this conversation
	 */
	public RemoteUser getRemoteUser(){
		return user;
	}
	
	/**
	 * methods of WindowListener
	 */
	@Override
	public void windowClosing(WindowEvent arg0) {
		this.chatGUI.removeElementListConversation(this);
	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}
}
